<?php


class Blackbird_ContentBlocksController extends Zend_Controller_Action
{
	function htmlAction()
	{
		$block = $this->_getParam('block');
		$this->view->block = $block;
		$this->view->content = $this->_replaceTags($block->content);
	}

	function bannersAction()
	{
		$block = $this->_getParam('block');
		$model = new Blackbird_Model_ContentBlocks();
		$this->view->block = $block;
		$this->view->banners = $model->fetchBanners($block->params);
	}

	function latestArticlesAction()
	{
		$block = $this->_getParam('block');
		$model = new Blackbird_Model_ContentBlocks();
		$this->view->block = $block;
		$this->view->articles = $model->fetchLatestArticles($block->params);
	}
	
	function latestPollAction()
	{
		$block = $this->_getParam('block');
		$model = new Blackbird_Model_ContentBlocks();
		$this->view->block = $block;
		$this->view->poll = $model->fetchLatestPoll($block->params);
	}

	function menuAction()
	{
		$block = $this->_getParam('block');
		$model = new Blackbird_Model_ContentBlocks();
		$this->view->block = $block;
		$this->view->menu = $model->fetchMenu($block->params);
	}

	private $_tags = array(
		'timestamp' => '_timestampTag',
		'user' => '_userTag',
	);

	function _replaceTags($content)
	{
		if (preg_match_all('/{([a-z0-9]+)(?: ([^}]+))?}/', $content, $matches))
		{
			$replacements = array();
			for ($i = 0, $matchesCount = count($matches[0]); $i < $matchesCount; ++$i)
			{
				$name = $matches[1][$i];
				if (isset($this->_tags[$name]))
				{
					$attribs = $matches[2][$i];
					$arguments = array();
					$start = 0;
					$length = 0;
					for ($c = 0, $characters = strlen($attribs); $c < $characters; ++$c)
					{
						if ("'" == $attribs{$c} || '"' == $attribs{$c})
						{
							$skip = 1;
							$delimiter = $attribs{$c};
							$start = ++$c;
						}
						else
						{
							$skip = 0;
							$delimiter = ',';
							$start = $c;
						}

						$length = 0;
						while ($c < $characters && $attribs{$c} != $delimiter)
						{
							++$length;
							++$c;
						}
						$arguments[] = substr($attribs, $start, $length);
						$start += $skip;
					}

					$tag = $matches[0][$i];
					$method = $this->_tags[$name];
					$replacements[$tag] = call_user_func_array(array($this, $method), $arguments);
				}
			}
			$content = str_replace(array_keys($replacements), array_values($replacements), $content);
		}
		return $content;
	}

	function _timestampTag($format = '%c', $timestamp = null)
	{
		return ucfirst(strftime($format, (null == $timestamp ? time() : strtotime($timestamp))));
	}

	function _userTag($properties)
	{
		$properties = func_get_args();
		$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity())
		{
			$identity = $auth->getIdentity();
			if (is_object($identity))
				$identity = get_object_vars($identity);
			$result = '';
			foreach ($properties as $property)
			{
				if (isset($identity[$property]))
					$result .= $identity[$property];
				else
					$result .= $property;
			}
			return $result;
		}
		return '';
	}
}

